package com.jiankangjin.transsystem.services.impl;


import java.util.ArrayList;
import java.util.List;


import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiankangjin.common.json.jackson.JSONUtil;

import com.jiankangjin.trading.dao.IDiagDataDao;
import com.jiankangjin.trading.dao.IOrderAffiliatedDao;
import com.jiankangjin.trading.dao.IOrderDataDao;
import com.jiankangjin.trading.dao.IOrderDetailDataDao;
import com.jiankangjin.trading.entity.OrderAffiliatedEntity;
import com.jiankangjin.trading.entity.OrderDataEntity;
import com.jiankangjin.trading.entity.OrderDetailDataEntity;
import com.jiankangjin.transsystem.services.UpdOrderInfoServices;
import com.jiankangjin.transsystem.vo.OrderDetailDataVo;
import com.jiankangjin.transsystem.vo.UpdOrderInfoVo;

/**
 * @author zfy 
 * @date 2014年12月12日 下午1:00:21 
 */
@Service
public class UpdOrderInfoServicesImpl implements UpdOrderInfoServices{

	@Autowired
	private IOrderDataDao iOrderDataDao;

	@Autowired
	private IOrderDetailDataDao iOrderDetailDataDao;

	@Autowired 
	private IDiagDataDao iDiagDataDao;

	@Autowired
	private  IOrderAffiliatedDao iOrderAffiliatedDao;
	
	/**
	 * @return
	 */
	@Override
	public UpdOrderInfoVo updOrderDetailInfo(String token, String order_id,
			String detail, String content, String comment, String sign) {
		UpdOrderInfoVo updOrderInfoVo = new UpdOrderInfoVo();
		try {
			// 查询订单号的商品明细是否存在，如不存在则返回不存在信息告知前置
			OrderDetailDataEntity orderDetailDataEntity = new OrderDetailDataEntity();
			orderDetailDataEntity.setOrdId(order_id);
			List<OrderDetailDataEntity> list = iOrderDetailDataDao.listByEntity(orderDetailDataEntity);
			if (list.size() == 0) {
				updOrderInfoVo.setMsg("您输入的订单号不存在，请重新输入订单号！");
				updOrderInfoVo.setResp_status("9999");
				return updOrderInfoVo;
			}
			//获取更改前的商品单价
        	Long oldGoods_price = (long) 0;
        	//获取要更改的商品单价
        	Long newGoods_price =(long) 0;
        	
			// 解析订单详情
			List<OrderDetailDataVo> orderDetailDataVos = JSONUtil.fromJSON(detail,new TypeReference<List<OrderDetailDataVo>>() {});
			OrderDetailDataEntity orderDetailData;
			// 循环遍历，更新商品明细
            for(int i=0;i<list.size();i++){
            	OrderDetailDataEntity orderDetail = (OrderDetailDataEntity)list.get(i);
            	//获取更改前的商品单价
            	 oldGoods_price = orderDetail.getPrice();
				for (OrderDetailDataVo orderDetailDataVo : orderDetailDataVos) {
					orderDetailData = new OrderDetailDataEntity();
					orderDetailData.setId(orderDetail.getId());
					orderDetailData.setOrdId(order_id);
					orderDetailData.setWaresId(orderDetailDataVo.getGoods_id());
					orderDetailData.setWaresType(orderDetailDataVo.getGoods_type());
					orderDetailData.setApCode(orderDetailDataVo.getAp_code());
					orderDetailData.setBatNo(orderDetailDataVo.getBat_no());
					orderDetailData.setLogCode(orderDetailDataVo.getLog_code());
					orderDetailData.setNdCode(orderDetailDataVo.getNd_code());
					orderDetailData.setMiCode(orderDetailDataVo.getMi_code());
					orderDetailData.setMiType(orderDetailDataVo.getMi_type());
					orderDetailData.setWaresName(orderDetailDataVo.getGoods_name());
					orderDetailData.setCmName(orderDetailDataVo.getCm_name());
					orderDetailData.setChName(orderDetailDataVo.getCh_name());
					orderDetailData.setStd(orderDetailDataVo.getStd());
					orderDetailData.setUnit(orderDetailDataVo.getUnit());
	            	orderDetailData.setPrice(new Long(orderDetailDataVo.getGoods_price()));
	            	newGoods_price =new Long(orderDetailDataVo.getGoods_price());
	            	orderDetailData.setCurrency("156");
	            	orderDetailData.setQuantity(new Long(orderDetailDataVo.getGoods_quantity()));
	            	orderDetailData.setAmount(new Long(orderDetailDataVo.getGoods_price() * orderDetailDataVo.getGoods_quantity()));
	            	orderDetailData.setShowUrl(orderDetailDataVo.getGoods_url());
					orderDetailData.setDetailComment(comment);
					// 更新商品明细
					iOrderDetailDataDao.update(orderDetailData);
				}
            }
			//通过orser_id 查出订单附属表（order_affiliated）的id
			OrderAffiliatedEntity orderAffiliated = new OrderAffiliatedEntity();
			orderAffiliated.setOrderId(order_id);
			OrderAffiliatedEntity orderAffiliatedEntity = iOrderAffiliatedDao.selectByEntity(orderAffiliated);
			
			//通过附属表的id更新附属表的is_tip
			OrderAffiliatedEntity AffiliatedId = new OrderAffiliatedEntity();
			AffiliatedId.setId(orderAffiliatedEntity.getId());
			AffiliatedId.setIsTip("0");
			// 更新is_tip
			iOrderAffiliatedDao.update(AffiliatedId);
           
			//更新后返回信息给前置接口
			List<OrderDetailDataEntity> orderDetail = iOrderDetailDataDao.listByEntity(orderDetailDataEntity);
			
			OrderDataEntity orderData =iOrderDataDao.selectByPrimaryKey(order_id);
			
			OrderAffiliatedEntity orderAffiliat = iOrderAffiliatedDao.selectByEntity(orderAffiliated);
           
			UpdOrderInfoVo.Trade.Channel[] channel1 = {};
			List<UpdOrderInfoVo.Trade> trade = new ArrayList<>();
			UpdOrderInfoVo.Trade.Goods[] good = new UpdOrderInfoVo.Trade.Goods[orderDetail.size()];
			for (int i = 0; i < orderDetail.size(); i++) {
				UpdOrderInfoVo.Trade.Goods good1 = updOrderInfoVo.new Trade().new Goods();
				OrderDetailDataEntity OrderDetailDataEntity = (OrderDetailDataEntity) orderDetail.get(i);
				good1.setGoods_name(OrderDetailDataEntity.getWaresName());
				good1.setGoods_price(new Long(OrderDetailDataEntity.getPrice()).intValue());
				good1.setGoods_quantity(new Long(OrderDetailDataEntity.getQuantity()).intValue());
				good1.setGoods_url(OrderDetailDataEntity.getShowUrl());
				good1.setId(OrderDetailDataEntity.getWaresId());
				good1.setStd(OrderDetailDataEntity.getStd());
				good1.setmI_type(OrderDetailDataEntity.getMiType());
				//如果原商品单价和闲更改的商品单价不一致，则明细后补标注为1，相同则为0
				if(oldGoods_price.equals(newGoods_price)){
					good1.setMxhb("0");
				}else{
					good1.setMxhb("1");
				}
				good[i] = good1;
			}
			String orderId = (String) orderAffiliat.getOrderId();
			int Order_fee =0;
			if(orderAffiliat.getDiscountFee() != null){
			Order_fee = Integer.parseInt(orderAffiliat.getDiscountFee());
			}
			String Buyer_cellphone = (String) orderAffiliat.getBuyerCellphone();
			String Buyer_name = (String) orderAffiliat.getBuyerName();
			String is_tip = (String) orderAffiliat.getIsTip();
            
			
			String state =(String)orderData.getOrdStat();
			String currency = (String)orderData.getOrdCry();
			String orderTime = (String)orderData.getOrdRegTime();
			String operatorId=(String)orderData.getOperatorId();
			String operatorName = (String)orderData.getOperatorName();

			UpdOrderInfoVo.Trade trade1 = new UpdOrderInfoVo().new Trade(
					orderId, state, Order_fee, currency, Buyer_cellphone,
					Buyer_name, orderTime, operatorId, operatorName, is_tip,
					good, channel1);
			trade.add(trade1);
			
			updOrderInfoVo.setTrade(trade);
			updOrderInfoVo.setMsg("商品明细更新成功");
			updOrderInfoVo.setResp_status("0000");
			updOrderInfoVo.setTrade_quantity(orderDetail.size()); //显示条数
			updOrderInfoVo.setSign("appSPOS_updOrderInfo_sign");
			return updOrderInfoVo;
		} catch (Exception e) {
			updOrderInfoVo.setMsg("系统错误！");
			updOrderInfoVo.setResp_status("9999");
			e.printStackTrace();
			return updOrderInfoVo;
		}
	}

}
